package com.aaa.cts.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cts.dao.DeptDao;
import com.aaa.cts.entity.Dept;
import com.aaa.cts.service.DeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门表(Dept)表服务实现类
 *
 * @author makejava
 * @since 2020-12-03 20:31:11
 */
@Service("deptService")
public class DeptServiceImpl implements DeptService {
    @Resource
    private DeptDao deptDao;

    /**
     * 通过ID查询单条数据
     *
     * @param deptId 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Integer deptId) {
        if(deptId==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        Dept dept = this.deptDao.queryById(deptId);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), dept);
    }

    /**
     * 通过实体作为筛选条件查询
     * @param dept
     * @return
     */
    @Override
    public Result queryList(Dept dept) {
        dept=new Dept();
        //设置删除状态
        dept.setDelFlag("0");
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), this.deptDao.queryAll(dept));
    }

    /**
     * 通过实体作为筛选条件查询(带分页)
     *
     * @param pageNo
     * @param pageSize
     * @param dept     实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, Dept dept) {
        //设置删除状态
        dept.setDelFlag("0");
        PageHelper.startPage(pageNo,pageSize);
        List<Dept> depts = this.deptDao.queryAll(dept);
        PageInfo<Dept> deptPageInfo = new PageInfo<>(depts);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), deptPageInfo);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public Result queryAllByLimit(int offset, int limit) {
        if(offset==0 || limit==0){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        List<Dept> depts = this.deptDao.queryAllByLimit(offset, limit);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), depts);
    }

    /**
     * 新增数据
     *
     * @param dept 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(Dept dept) {
        //设置删除状态
        dept.setDelFlag("0");
        int insert = this.deptDao.insert(dept);
        if(insert<=0){
            throw new CustomException(ExceptionConstant.UNKOWN_EXCEPTION.getErrorCode(),
                    ExceptionConstant.UNKOWN_EXCEPTION.getErrorMessage());
        }
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg());
    }

    /**
     * 修改数据
     *
     * @param dept 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(Dept dept) {
        int update = this.deptDao.update(dept);
        if(update<=0){
            throw new CustomException(ExceptionConstant.UNKOWN_EXCEPTION.getErrorCode(),
                    ExceptionConstant.UNKOWN_EXCEPTION.getErrorMessage());
        }
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg());
    }

    /**
     * 通过主键删除数据
     *
     * @param deptId 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(Integer deptId) {
        boolean b = this.deptDao.deleteById(deptId) > 0;
        if(!b){
            throw new CustomException(ExceptionConstant.UNKOWN_EXCEPTION.getErrorCode(),
                    ExceptionConstant.UNKOWN_EXCEPTION.getErrorMessage());
        }
        return new Result<Boolean>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),b);
    }
}